package com.springcloudalibaba.apiPay.configurer.properties;

import com.alibaba.fastjson.JSONObject;
import com.springcloudalibaba.bclass.base.BaseException;
import com.springcloudalibaba.apiPay.configurer.yuml.ProductEntity;
import com.springcloudalibaba.apiPay.configurer.yuml.ProductYml;
import com.springcloudalibaba.apiPay.enums.ClientEnum;
import com.springcloudalibaba.apiPay.util.BusinessTypeUtil;
import com.springcloudalibaba.apiPay.util.WxStrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;

/**
 * @描述: 预支付信息
 * @作者: lixing
 * @日期 2021/7/9 9:27
 */
@Component
public class AdvancePaymentPropertiesConfig {
    /** 微信、支付宝相关配置参数 */
    @Autowired
    private AliBabaLhgyyProperties aliBabaLhgyyProperties;
    @Autowired
    private WeChatLhgyyProperties weChatLhgyyProperties;

    /** 聚合支付业务类型 */
    @Autowired
    private ProductYml productYml;

    /**
     * @描述 根据聚合支付业务类型读取相关参数
     * @作者 lixing
     * @日期 2021/7/8 11:13
     * @Param [businessType 100000 前3位100表示项目编号，后3位000表示业务场景编号]
     * @return com.alibaba.fastjson.JSONObject
     */
    public AdvancePaymentProperties getParamsByBusinessType(String client, String businessType, long userId) {
        ProductEntity productEntity = BusinessTypeUtil.getProductById("productId", productYml.getGoods());
        if (null == productEntity) {
            throw new BaseException("支付参数非法");
        }
        //
        AdvancePaymentProperties advancePaymentProperties = new AdvancePaymentProperties();
        advancePaymentProperties.setProductId("productId");
        //
        // TODO 订单配置信息,每添加一种聚合支付类型就要配置一个枚举
        //        if (productId.equals( BusinessTypeEnum.UP_COMPANY_USER.getBusinessType() )) {
        //        } else {
        //            throw new TradeException("支付业务类型非法");
        //        }
        BigDecimal totalAmount = new BigDecimal(productEntity.getTotalAmount());;
        if (client.equals(ClientEnum.WECHAT.getClient())) {
            // 微信支付金额单位为：分，支付宝支付金额单位为：元
            totalAmount = totalAmount.multiply(new BigDecimal("100"));
        }
        // 订单标题
        advancePaymentProperties.setSubject(productEntity.getSubject());
        // 订单描述
        advancePaymentProperties.setBody(productEntity.getBody());
        // 商品id
        advancePaymentProperties.setProductId(productEntity.getProductId());
        // 订单金额
        advancePaymentProperties.setTotalAmount(WxStrUtil.formatNumStr(totalAmount.toString(), 0));
        // 收款账户名称
        advancePaymentProperties.setCollectionAccountName(productEntity.getCollectionAccountName());
        // 公共回传参数，如果请求时传递了该参数，则返回给商户时会在异步通知时将该参数原样返回。本参数必须进行UrlEncode之后才可以发送给支付宝
        // 示例值：merchantBizType%3d3C%26merchantBizNo%3d2016010101111
        JSONObject passbackParamsObj = new JSONObject();
        passbackParamsObj.put("client", client);
        passbackParamsObj.put("productId", "productId");
        passbackParamsObj.put("userId", userId);
        passbackParamsObj.put("sourceFrom", "sourceFrom");
        advancePaymentProperties.setPassbackParams(passbackParamsObj.toString());
        // 支付客户端类型
        if (client.equals(ClientEnum.WECHAT.getClient())) {
            advancePaymentProperties.setWechatAppId(weChatLhgyyProperties.getAppId().trim());
            advancePaymentProperties.setWechatAppSecret(weChatLhgyyProperties.getAppSecret().trim());
            advancePaymentProperties.setWechatMchId(weChatLhgyyProperties.getMchId().trim());
            advancePaymentProperties.setWechatKey(weChatLhgyyProperties.getKey().trim());
            advancePaymentProperties.setWechatNotifyUrl(weChatLhgyyProperties.getNotifyUrl().trim());
        } else if (client.equals(ClientEnum.ALIBABA.getClient())) {
            advancePaymentProperties.setAlibabaAppId(aliBabaLhgyyProperties.getAppId().trim());
            advancePaymentProperties.setAlibabaSignType(aliBabaLhgyyProperties.getSignType().trim());
            advancePaymentProperties.setAlibabaRsaPrivateKey(aliBabaLhgyyProperties.getRsaPrivateKey().trim());
            advancePaymentProperties.setAlibabaRsaPublicKey(aliBabaLhgyyProperties.getRsaPublicKey().trim());
            advancePaymentProperties.setAlibabaNotifyUrl(aliBabaLhgyyProperties.getNotifyUrl().trim());
            advancePaymentProperties.setAlibabaReturnUrl(aliBabaLhgyyProperties.getReturnUrl().trim());
            advancePaymentProperties.setAlibabaGatewayUrl(aliBabaLhgyyProperties.getGatewayUrl().trim());
            advancePaymentProperties.setAlibabaCharset(aliBabaLhgyyProperties.getCharset().trim());
            advancePaymentProperties.setAlibabaFormat(aliBabaLhgyyProperties.getFormat().trim());
        } else {
            throw new BaseException("支付终端类型非法");
        }
        return advancePaymentProperties;
    }
}
